﻿namespace Rzrlf.SP.Innovator.WebParts.ChangeRequestEventReceiver
{
    public class ChangeRequestEventReceiver : Microsoft.SharePoint.Workflow.SPWorkflowEventReceiver
    {
        public override void WorkflowCompleted(Microsoft.SharePoint.Workflow.SPWorkflowEventProperties properties)
        {
            Microsoft.SharePoint.SPSite site = new Microsoft.SharePoint.SPSite(properties.WebUrl);

            Microsoft.SharePoint.SPWeb web = site.OpenWeb();

            Microsoft.SharePoint.SPList list = web.Lists["Change Requests"];

            bool isProcessed = false;

            foreach (Microsoft.SharePoint.SPListItem item in list.Items)
            {
                foreach (Microsoft.SharePoint.Workflow.SPWorkflow workflow in item.Workflows)
                {
                    if (workflow.InstanceId == properties.InstanceId)
                    {
                        string id = item.GetFormattedValue("Part Id");

                        string description = item.GetFormattedValue("Description");

                        string owner = item.GetFormattedValue("Owner");

                        string reason = item.GetFormattedValue("Reason");

                        Rzrlf.SP.Innovator.WebParts.Utils.IomUtil.CreatePartECO(id, description, reason, owner);

                        isProcessed = true;

                        break;
                    }
                }

                if (isProcessed)
                {
                    break;
                }
            }

            base.WorkflowCompleted(properties);
        }
    }
}
